﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using System.Xml;
using System.IO;

namespace OrderManagement
{
   public class OrderService
    {
        private List<Order> orders;

        public OrderService(List<Order> orders)
        {
            this.orders = orders;
        }
        public void SortOrder()   //订单排序
        {
            if (this.GetOrders() == null)
                return;
            orders.Sort((o1, o2) => Convert.ToInt32(o1.ID)-Convert.ToInt32(o2.ID));
        }
        public void SortOrder(Comparison<Order> action) //订单排序
        {
            orders.Sort(action);
        }
        public List<Order> GetOrders()   //得到orders
        {
            return orders;
        }
        public void AddOrder(Order order)  //添加订单
        {
            foreach(Order m in orders)
            {
                if (m.Equals(order))
                    throw new Exception("添加订单重复");
            }
            this.orders.Add(order);
            //this.SortOrder();        
        }
        public bool DeleteOrder(String ID)   //删除订单
        {
            bool flag = true;
            int index = orders.FindIndex((a) => a.ID == ID);
            if (index == -1)
            {
                flag = false;
                throw new Exception("查无此订单");
            }
            else
            {
                orders.RemoveAt(index);
            }
            return flag;
        }
        public bool ChangeOrder(String ID,Order order)  //把需要修改的订单改为指定订单
        {
            bool flag = true;
            int index= orders.FindIndex((a) => a.ID == ID);  //index=-1则表示未能找到该订单
            if (index == -1)
            {
                flag = false;
                throw new Exception("订单修改失败");
                
            }
            else
            {
                orders[index] = order;  //修改订单
            }
            return flag;
                

        }
        public List<Order> OrderCheckID(String ID) //按照订单ID进行查询
        {
            //bool flag = true;
            var checkorder = from s in orders
                             where s.ID == "ID"
                             orderby s.ID
                             select s;
            List<Order>list = checkorder.ToList();
            if (checkorder == null)
            {
               // flag = false;
                throw new Exception("查无此订单号");
            }
           
            return list;
           // return flag;
        }
        public List<Order> OrderCheckPrice(int min,int max) //按照订单金额查询
        {
            var checkorder = from s in orders
                             where s.OrderDetails.Price >= min && s.OrderDetails.Price < max
                             orderby s.ID
                             select s;
            List<Order> list = checkorder.ToList();
            if (list == null)
                throw new Exception("无此金额范围内的订单");
            else
                return list;
        }
        public List<Order> OrderCheckName(String Name) //按照客户姓名的查询
        {
            var checkorder = from s in orders
                             where s.Customer.Name == Name
                             orderby s.ID
                             select s;
            List<Order> list = checkorder.ToList();
            if (list == null)
                throw new Exception("查无此顾客");
            else
                return list;
        }
        public List<Order> OrderCheckGood(String Name)  //查询物品名称
        {
            var checkorder = from s in orders
                             where s.OrderDetails.goods.FindIndex((m) => m.Name == Name) != -1
                             orderby s.ID
                             select s;
            List<Order> list = checkorder.ToList();
            if (list == null)
                throw new Exception("查无此物品");
            else
                return list;
        }
        public void Export()
        {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<Order>));
            using (FileStream fs = new FileStream(@"D:\Test\order.xml", FileMode.Create))
            {
                xmlSerializer.Serialize(fs, this.orders);
            }
        }

        public List<Order> Import()
        {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<Order>));
            using (FileStream fs = new FileStream(@"D:\Test\order.xml", FileMode.Open))
            {
                List<Order> orders = (List<Order>)xmlSerializer.Deserialize(fs);
                return orders;
            }
        }

    }
}
